package com.ylxl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylxl.common.annotation.Log;
import com.ylxl.common.enums.BusinessType;
import com.ylxl.common.result.AjaxResult;
import com.ylxl.common.result.BaseController;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.reponse.ViewScreenVO;
import com.ylxl.entity.request.DeleteRequest;
import com.ylxl.entity.request.QueryViewScreen;
import com.ylxl.entity.request.ViewScreenCmdOrMdy;
import com.ylxl.entity.request.ViewScreenStatusMdy;
import com.ylxl.service.IViewScreenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther Gilbert
 * @Date 2023/5/30 19:51
 */
@Slf4j
@RestController
@RequestMapping("/viewScreen")
@Api(tags = "显示屏管理", hidden = true)
public class ViewScreenController extends BaseController {
    @Autowired
    private IViewScreenService viewScreenService;


    @Log(title = "分页查询显示屏列表信息", businessType = BusinessType.SELECT)
    @PostMapping("/queryViewScreenList")
    @ApiOperation(value = "分页查询显示屏列表信息", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ViewScreenVO.class),})
    public AjaxResult queryViewScreenList(@RequestBody QueryViewScreen queryViewScreen) {
        AjaxResult ajax = AjaxResult.success();
        try {
            //进行分页操作
            PageInfo<ViewScreenVO> pageInfo = new PageInfo<>();
            if (StringUtils.isNotEmpty(queryViewScreen.getPageNum()) && StringUtils.isNotEmpty(queryViewScreen.getPageSize())) {
                PageHelper.startPage(queryViewScreen.getPageNum(), queryViewScreen.getPageSize());
            }
            List<ViewScreenVO> viewScreenVOS = viewScreenService.selectViewScreenList(queryViewScreen);
            pageInfo = new PageInfo<>(viewScreenVOS);
            ajax.put(AjaxResult.DATA_TAG, viewScreenVOS);
            ajax.put(AjaxResult.TOTAL, pageInfo.getTotal());
        } catch (Exception e) {
            log.error("查询显示屏列表日志信息，异常信息：{}", e);
            return toAjax(false);
        }
        return ajax;
    }

    @Log(title = "显示屏保存", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    @ApiOperation(value = "显示屏保存", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult add(@RequestBody ViewScreenCmdOrMdy viewScreenCmdOrMdy){
        return toAjax(viewScreenService.insertViewScreen(viewScreenCmdOrMdy));
    }

    @Log(title = "显示屏编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation(value = "显示屏编辑", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult update(@RequestBody ViewScreenCmdOrMdy viewScreenCmdOrMdy){
        return toAjax(viewScreenService.updateViewScreen(viewScreenCmdOrMdy));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "显示屏根据编号(id)查询明细", response = AjaxResult.class)
    @Log(title = "显示屏根据编号(id)查询明细", businessType = BusinessType.SELECT)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ViewScreenVO.class),})
    public AjaxResult getInfo(@PathVariable(value = "id", required = true) Integer id) {
        return AjaxResult.success(viewScreenService.selctById(id));
    }


    @PostMapping("/remove")
    @ApiOperation(value = "显示屏根据ids数组删除", response = AjaxResult.class)
    @Log(title = "显示屏根据ids数组删除", businessType = BusinessType.DELETE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult remove(@RequestBody DeleteRequest deleteRequest) {
        return AjaxResult.success(viewScreenService.deleteByIds(deleteRequest.getIds()));
    }
    @Log(title = "显示屏启用和禁用", businessType = BusinessType.UPDATE)
    @PostMapping("/disabled")
    @ApiOperation(value = "显示屏启用和禁用", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult disabled(@RequestBody ViewScreenStatusMdy viewScreenStatusMdy){
        return toAjax(viewScreenService.updateDisabled(viewScreenStatusMdy));
    }
}

